{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from xgboost import XGBClassifier\n",
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "import itertools\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read OK!\n"
     ]
    }
   ],
   "source": [
    "train = pd.read_table('train/subsidy_train.txt',sep=',',header=-1)\n",
    "train.columns = ['id','label']\n",
    "test = pd.read_table('test/studentID_test.txt',sep=',',header=-1)\n",
    "test.columns = ['id']\n",
    "test['label'] = np.nan\n",
    "\n",
    "#train_test = pd.concat([train,test])\n",
    "train_test=train\n",
    "\n",
    "del train\n",
    "del test\n",
    "\n",
    "'''\n",
    "score_train = pd.read_table('train/score_train.txt',sep=',',header=-1)\n",
    "score_train.columns = ['id','college','rank']\n",
    "score_test = pd.read_table('test/score_test.txt',sep=',',header=-1)\n",
    "score_test.columns = ['id','college','rank']\n",
    "score_train_test = pd.concat([score_train,score_test])\n",
    "\n",
    "college = pd.DataFrame(score_train_test.groupby(['college'])['rank'].max())\n",
    "college.to_csv('input/college.csv',index=True)\n",
    "college = pd.read_csv('input/college.csv')\n",
    "college.columns = ['college','total_people']\n",
    "\n",
    "score_train_test = pd.merge(score_train_test, college, how='left',on='college')\n",
    "score_train_test['rank_percent'] = score_train_test['rank']/score_train_test['total_people']\n",
    "train_test = pd.merge(train_test,score_train_test,how='left',on='id')\n",
    "'''\n",
    "\n",
    "card_train = pd.read_table('train/card_train.txt',sep=',',header=-1)\n",
    "card_train.columns = ['id','pos','place','consume','time','price','rest']\n",
    "#card_test = pd.read_table('test/card_test.txt',sep=',',header=-1)\n",
    "#card_test.columns = ['id','pos','place','consume','time','price','rest']\n",
    "#\n",
    "#card_train_test = pd.concat([card_train,card_test])\n",
    "card_train_test=card_train\n",
    "print \"Read OK!\"\n",
    "\n",
    "##release memery\n",
    "del card_train\n",
    "#del card_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#card_train_test=card_train_test.drop_duplicates()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#card_train_test.drop(['place','time'],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12455558, 7)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "card_train_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>pos</th>\n",
       "      <th>place</th>\n",
       "      <th>consume</th>\n",
       "      <th>time</th>\n",
       "      <th>price</th>\n",
       "      <th>rest</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1006</td>\n",
       "      <td>POS消费</td>\n",
       "      <td>地点551</td>\n",
       "      <td>淋浴</td>\n",
       "      <td>2013/09/01 00:00:32</td>\n",
       "      <td>0.50</td>\n",
       "      <td>124.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1006</td>\n",
       "      <td>POS消费</td>\n",
       "      <td>地点551</td>\n",
       "      <td>淋浴</td>\n",
       "      <td>2013/09/01 00:00:32</td>\n",
       "      <td>0.50</td>\n",
       "      <td>124.90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1968</td>\n",
       "      <td>POS消费</td>\n",
       "      <td>地点159</td>\n",
       "      <td>淋浴</td>\n",
       "      <td>2013/09/01 00:00:39</td>\n",
       "      <td>0.10</td>\n",
       "      <td>200.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1968</td>\n",
       "      <td>POS消费</td>\n",
       "      <td>地点159</td>\n",
       "      <td>淋浴</td>\n",
       "      <td>2013/09/01 00:00:39</td>\n",
       "      <td>0.10</td>\n",
       "      <td>200.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1406</td>\n",
       "      <td>POS消费</td>\n",
       "      <td>地点660</td>\n",
       "      <td>开水</td>\n",
       "      <td>2013/09/01 00:00:40</td>\n",
       "      <td>0.01</td>\n",
       "      <td>374.42</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     id    pos  place consume                 time  price    rest\n",
       "0  1006  POS消费  地点551      淋浴  2013/09/01 00:00:32   0.50  124.90\n",
       "1  1006  POS消费  地点551      淋浴  2013/09/01 00:00:32   0.50  124.90\n",
       "2  1968  POS消费  地点159      淋浴  2013/09/01 00:00:39   0.10  200.14\n",
       "3  1968  POS消费  地点159      淋浴  2013/09/01 00:00:39   0.10  200.14\n",
       "4  1406  POS消费  地点660      开水  2013/09/01 00:00:40   0.01  374.42"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "card_train_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'地点21', u'地点91', u'地点232', u'地点188', u'地点192', u'地点118', u'地点26',\n",
       "       u'地点996', u'地点6', u'地点83',\n",
       "       ...\n",
       "       u'地点1412', u'地点2332', u'地点2696', u'地点2697', u'地点2692', u'地点2693',\n",
       "       u'地点2691', u'地点2699', u'地点2686', u'地点2694'],\n",
       "      dtype='object', length=2468)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "card_train_test['place'].value_counts().index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "place_info=card_train_test['place'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "place_more=place_info[place_info>5000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for var in place_more.index:\n",
    "    place = card_train_test[card_train_test.place == var]\n",
    "    \n",
    "    feature_p = pd.DataFrame(place.groupby(['id'])['pos'].count())\n",
    "    feature_p['%s_sum'%var]=place.groupby(['id'])['price'].sum()\n",
    "    feature_p['%s_avg'%var]=place.groupby(['id'])['price'].mean()\n",
    "    feature_p['%s_max'%var]=place.groupby(['id'])['price'].max()\n",
    "    feature_p['%s_min'%var]=place.groupby(['id'])['price'].min()\n",
    "    feature_p['%s_median'%var]=place.groupby(['id'])['price'].median()\n",
    "    \n",
    "    del place\n",
    "    feature_p.to_csv('input/card_%sfeature.csv'%var,index=True)\n",
    "    feature_p=pd.read_csv('input/card_%sfeature.csv'%var)\n",
    "    feature_p=feature_p.rename(columns={'pos' : '%s_count'%var})\n",
    "    \n",
    "    train_test = pd.merge(train_test, feature_p, how='left',on='id') \n",
    "    del feature_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#del card_train_test\n",
    "train_test=train_test.fillna(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>label</th>\n",
       "      <th>地点21_count</th>\n",
       "      <th>地点21_sum</th>\n",
       "      <th>地点21_avg</th>\n",
       "      <th>地点21_max</th>\n",
       "      <th>地点21_min</th>\n",
       "      <th>地点21_median</th>\n",
       "      <th>地点91_count</th>\n",
       "      <th>地点91_sum</th>\n",
       "      <th>...</th>\n",
       "      <th>地点145_avg</th>\n",
       "      <th>地点145_max</th>\n",
       "      <th>地点145_min</th>\n",
       "      <th>地点145_median</th>\n",
       "      <th>地点355_count</th>\n",
       "      <th>地点355_sum</th>\n",
       "      <th>地点355_avg</th>\n",
       "      <th>地点355_max</th>\n",
       "      <th>地点355_min</th>\n",
       "      <th>地点355_median</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>94.0</td>\n",
       "      <td>421.50</td>\n",
       "      <td>4.484043</td>\n",
       "      <td>23.1</td>\n",
       "      <td>0.1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>378.49</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>260.42</td>\n",
       "      <td>3.945758</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>72.40</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>651.0</td>\n",
       "      <td>2424.96</td>\n",
       "      <td>3.724977</td>\n",
       "      <td>31.5</td>\n",
       "      <td>-5.0</td>\n",
       "      <td>3.1</td>\n",
       "      <td>262.0</td>\n",
       "      <td>2203.45</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>1047.0</td>\n",
       "      <td>4741.29</td>\n",
       "      <td>4.528453</td>\n",
       "      <td>37.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>142.10</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>121.0</td>\n",
       "      <td>747.43</td>\n",
       "      <td>6.177107</td>\n",
       "      <td>30.0</td>\n",
       "      <td>-6.8</td>\n",
       "      <td>5.1</td>\n",
       "      <td>88.0</td>\n",
       "      <td>445.30</td>\n",
       "      <td>...</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "      <td>-1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 1820 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  label  地点21_count  地点21_sum  地点21_avg  地点21_max  地点21_min  地点21_median  \\\n",
       "0   0      0        94.0    421.50  4.484043      23.1       0.1          3.0   \n",
       "1   1      0        66.0    260.42  3.945758      20.0       0.1          3.0   \n",
       "2   8      0       651.0   2424.96  3.724977      31.5      -5.0          3.1   \n",
       "3   9      0      1047.0   4741.29  4.528453      37.0       0.2          3.0   \n",
       "4  10      0       121.0    747.43  6.177107      30.0      -6.8          5.1   \n",
       "\n",
       "   地点91_count  地点91_sum      ...       地点145_avg  地点145_max  地点145_min  \\\n",
       "0        67.0    378.49      ...            -1.0       -1.0       -1.0   \n",
       "1        14.0     72.40      ...            -1.0       -1.0       -1.0   \n",
       "2       262.0   2203.45      ...            -1.0       -1.0       -1.0   \n",
       "3        25.0    142.10      ...            -1.0       -1.0       -1.0   \n",
       "4        88.0    445.30      ...            -1.0       -1.0       -1.0   \n",
       "\n",
       "   地点145_median  地点355_count  地点355_sum  地点355_avg  地点355_max  地点355_min  \\\n",
       "0          -1.0         -1.0       -1.0       -1.0       -1.0       -1.0   \n",
       "1          -1.0         -1.0       -1.0       -1.0       -1.0       -1.0   \n",
       "2          -1.0         -1.0       -1.0       -1.0       -1.0       -1.0   \n",
       "3          -1.0         -1.0       -1.0       -1.0       -1.0       -1.0   \n",
       "4          -1.0         -1.0       -1.0       -1.0       -1.0       -1.0   \n",
       "\n",
       "   地点355_median  \n",
       "0          -1.0  \n",
       "1          -1.0  \n",
       "2          -1.0  \n",
       "3          -1.0  \n",
       "4          -1.0  \n",
       "\n",
       "[5 rows x 1820 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## Evaluation \n",
    "def f1_macro(label_truth, predictions):\n",
    "    df=pd.DataFrame(columns=[\"subsidy_x\",\"subsidy_y\"])\n",
    "    df.subsidy_y=predictions\n",
    "    df.subsidy_x=np.array(label_truth)\n",
    "    df.subsidy_y = df.subsidy_y.apply(lambda x:int(x))\n",
    "\n",
    "    \n",
    "    correct = df[df['subsidy_x'] == df['subsidy_y']]\n",
    "    s = 0\n",
    "    for i in [1000, 1500, 2000]:\n",
    "        #print i\n",
    "        r = float(sum(correct['subsidy_y'] == i))/sum(df['subsidy_x'] == i)\n",
    "        #print r\n",
    "        p = float(sum(correct['subsidy_y'] == i))/sum(df['subsidy_y'] == i)        \n",
    "        #print p\n",
    "        f = r*p*2/(r+p)\n",
    "        if not np.isnan(f):\n",
    "            s += (float(sum(df['subsidy_x'] == i))/df.shape[0])*f\n",
    "    return s\n",
    "\n",
    "def plot_confusion_matrix(cm, classes,\n",
    "                          normalize=False,\n",
    "                          title='Confusion matrix',\n",
    "                          cmap=plt.cm.Blues):\n",
    "    \"\"\"\n",
    "    This function prints and plots the confusion matrix.\n",
    "    Normalization can be applied by setting `normalize=True`.\n",
    "    \"\"\"\n",
    "    plt.imshow(cm, interpolation='nearest', cmap=cmap)\n",
    "    plt.title(title)\n",
    "    plt.colorbar()\n",
    "    tick_marks = np.arange(len(classes))\n",
    "    plt.xticks(tick_marks, classes, rotation=45)\n",
    "    plt.yticks(tick_marks, classes)\n",
    "\n",
    "    if normalize:\n",
    "        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n",
    "        print(\"Normalized confusion matrix\")\n",
    "    else:\n",
    "        print('Confusion matrix, without normalization')\n",
    "\n",
    "    print(cm)\n",
    "\n",
    "    thresh = cm.max() / 2.\n",
    "    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n",
    "        plt.text(j, i, cm[i, j],\n",
    "                 horizontalalignment=\"center\",\n",
    "                 color=\"white\" if cm[i, j] > thresh else \"black\")\n",
    "\n",
    "    plt.tight_layout()\n",
    "    plt.ylabel('True label')\n",
    "    plt.xlabel('Predicted label')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = train_test.ix[:, train_test.columns != 'label']\n",
    "y = train_test.ix[:, train_test.columns == 'label']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.3, random_state = 42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/kuhung/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  from ipykernel import kernelapp as app\n"
     ]
    }
   ],
   "source": [
    "train=X_train\n",
    "train['label']=y_train\n",
    "\n",
    "test=X_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#nice_feature=pd.read_csv('input/nice_feature.csv',header=None,index_col=0)\n",
    "\n",
    "target = 'label'\n",
    "IDcol = 'id'\n",
    "ids = test['id'].values\n",
    "\n",
    "all_feature = [x for x in train.columns if x not in [target]]\n",
    "predictors = [ x for x in all_feature if x in feature_imp_place20.feature.values]\n",
    "#predictors = [ x for x in all_feature if x in nice_feature.index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "predictors_noID = [x for x in predictors if x != 'id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       6538\n",
       "1000     524\n",
       "1500     308\n",
       "2000     249\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train['label'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Number of normal transactions: ', 1081)\n",
      "('Number of fraud transactions: ', 1081)\n",
      "('Total number of transactions in resampled data: ', 2162)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# tips：\n",
    "# 尝试欠采样和smoth采样\n",
    "\n",
    "# undersample\n",
    "train.to_csv('input/train_time-part.csv',index=False)\n",
    "train=pd.read_csv('input/train_time-part.csv')\n",
    "\n",
    "number_records_fraud = len(train[train.label != 0])\n",
    "fraud_indices = np.array(train[train.label != 0].index)\n",
    "\n",
    "# Picking the indices of the normal classes\n",
    "normal_indices = train[train.label == 0].index\n",
    "\n",
    "# Out of the indices we picked, randomly select \"x\" number (number_records_fraud)\n",
    "random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace = False)\n",
    "random_normal_indices = np.array(random_normal_indices)\n",
    "\n",
    "# Appending the 2 indices\n",
    "under_sample_indices = np.concatenate([fraud_indices,random_normal_indices])\n",
    "\n",
    "# Under sample dataset\n",
    "under_sample_data = train.iloc[under_sample_indices,:]\n",
    "\n",
    "X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'label']\n",
    "y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'label']\n",
    "\n",
    "# Showing ratio\n",
    "print(\"Number of normal transactions: \", len(under_sample_data[under_sample_data.label == 0]))\n",
    "print(\"Number of fraud transactions: \", len(under_sample_data[under_sample_data.label != 0]))\n",
    "print(\"Total number of transactions in resampled data: \", len(under_sample_data))\n",
    "\n",
    "\n",
    "# Oversample\n",
    "Oversampling1000 = train.loc[train.label == 1000]\n",
    "Oversampling1500 = train.loc[train.label == 1500]\n",
    "Oversampling2000 = train.loc[train.label == 2000]\n",
    "'''\n",
    "for i in range(6):\n",
    "    train = train.append(Oversampling1000)\n",
    "for j in range(10):\n",
    "    train = train.append(Oversampling1500)\n",
    "for k in range(12):\n",
    "    train = train.append(Oversampling2000)\n",
    "'''\n",
    "for i in range(5):\n",
    "    train = train.append(Oversampling1000)\n",
    "for j in range(8):\n",
    "    train = train.append(Oversampling1500)\n",
    "for k in range(10):\n",
    "    train = train.append(Oversampling2000)\n",
    "    \n",
    "train = train[train['label'].notnull()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(15193, 1820)"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.02564783038258662"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# model\n",
    "\n",
    "clf=XGBClassifier(seed=42)\n",
    "#clf = RandomForestClassifier()\n",
    "#clf = GradientBoostingClassifier(random_state=42,n_estimators=200)\n",
    "#clf = clf.fit(np.log(X_undersample[predictors]),y_undersample[target])\n",
    "\n",
    "clf = clf.fit(np.log(train[predictors_noID]),train[target])\n",
    "\n",
    "result = clf.predict(np.log(test[predictors_noID]))\n",
    "f1_macro(y_test,result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feat_imp=pd.Series(clf.booster().get_fscore()).sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "feature_imp_place20=feat_imp.head(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "地点21_count      83\n",
       "地点21_avg        47\n",
       "地点829_sum       32\n",
       "地点818_max       19\n",
       "地点829_min       18\n",
       "地点213_avg       16\n",
       "地点72_count      16\n",
       "地点283_count     16\n",
       "地点91_count      16\n",
       "地点245_avg       15\n",
       "地点283_sum       15\n",
       "地点213_count     15\n",
       "地点65_max        15\n",
       "地点161_sum       15\n",
       "地点996_count     15\n",
       "地点277_median    14\n",
       "地点842_max       14\n",
       "地点75_max        14\n",
       "地点263_avg       14\n",
       "地点840_count     14\n",
       "地点227_sum       14\n",
       "地点21_min        14\n",
       "地点844_count     13\n",
       "地点268_max       13\n",
       "地点72_min        13\n",
       "地点996_avg       13\n",
       "地点272_sum       12\n",
       "地点232_median    12\n",
       "地点21_max        12\n",
       "地点26_min        12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_imp_place20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'地点21_count', u'地点21_avg', u'地点829_sum', u'地点818_max', u'地点829_min',\n",
       "       u'地点213_avg', u'地点72_count', u'地点283_count', u'地点91_count',\n",
       "       u'地点245_avg', u'地点283_sum', u'地点213_count', u'地点65_max', u'地点161_sum',\n",
       "       u'地点996_count', u'地点277_median', u'地点842_max', u'地点75_max',\n",
       "       u'地点263_avg', u'地点840_count', u'地点227_sum', u'地点21_min', u'地点844_count',\n",
       "       u'地点268_max', u'地点72_min', u'地点996_avg', u'地点272_sum', u'地点232_median',\n",
       "       u'地点21_max', u'地点26_min'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_imp_place20.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feature_imp_place20=feature_imp_place20.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "'''\n",
    "## 这里的编码方式待处理，否则无法输出。简单起见，我把以下feature复制出去。\n",
    "feature_imp_place20.to_csv('input/feature_imp_place30.csv')\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "feature_imp_place20=pd.read_csv('input/feature_imp_place20.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['\\xe5\\x9c\\xb0\\xe7\\x82\\xb921_count',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb921_avg', '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9829_sum',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9818_max',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9829_min',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9213_avg',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb972_count',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9283_count',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb991_count',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9245_avg',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9283_sum',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9213_count',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb965_max', '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9161_sum',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9996_count',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9277_median',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9842_max', '\\xe5\\x9c\\xb0\\xe7\\x82\\xb975_max',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9263_avg',\n",
       "       '\\xe5\\x9c\\xb0\\xe7\\x82\\xb9840_count'], dtype=object)"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_imp_place20.feature.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
